/**
 * 
 */

package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.RequestPackage;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.UrlWrapper;

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:修改用户头像
 *
 * @author littlechuanxia
 * @since 2015年6月30日 上午10:16:00
 */
public class ModifyUserHeadTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    private File mPhotoFile;

    public ModifyUserHeadResponse request(String username, File file) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> params = new HashMap<String, Object>();
        ModifyUserHeadRequestPackage req = new ModifyUserHeadRequestPackage();
        req.setUserName(username);
        ModifyUserHeadResponse respObj = new ModifyUserHeadResponse();
        ResponsePackage resp = new ModifyUserHeadResponsePackage();
        params.put("cmd", ProtocolCmd.CMD_UPDATE_USER_PHOTO);
        params.put("username", username);
        // params.put("photo", file);
        mPhotoFile = file;
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_UPDATE_USER_PHOTO);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class ModifyUserHeadRequestPackage extends HttpBaseRequestPackage {

        @Override
        public HttpEntity getPostRequestEntity() {
            MultipartEntity entity = new MultipartEntity();
            if (mPhotoFile != null) {
                try {
                    entity.addPart("photo", new FileBody(mPhotoFile));
                    LogUtil.d("test", mPhotoFile.toString());
                    return entity;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
            return null;
        }

        public String getUrl() {
            return UrlWrapper.SERVER_HOST + getGetRequestParams();
        }

        @Override
        public int getRequestType() {
            return RequestPackage.TYPE_POST;
        }
    }

    class ModifyUserHeadResponsePackage extends HttpBaseResponsePackage<ModifyUserHeadResponse> {

        @Override
        protected void handleResponse(ModifyUserHeadResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "ModifyUserHeadTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (ProtocolCmd.CMD_UPDATE_USER_PHOTO.equalsIgnoreCase(cmd)) {
                        if (response.isUseful()) {
                            response.setSucceed(true);
                        } else {
                            response.setSucceed(false);
                            response.setMsg(prompt);
                        }
                        response.setOk(true);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class ModifyUserHeadResponse extends ResponseMessage {
        boolean isSucceed;

        String msg;

        public boolean isSucceed() {
            return isSucceed;
        }

        public void setSucceed(boolean isSucceed) {
            this.isSucceed = isSucceed;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
